﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;

namespace WinHBM.Utils
{
    public class UtilSocket
    {


        public static Socket ConnectTo(string ip,int port)
        {
            Socket socket = NewSocketObj();
            //创建对象IP/PORT
            IPEndPoint point = new IPEndPoint(IPAddress.Parse(ip), port);
            socket.Connect(point);
            return socket;
        }


        public static Socket ListenOn(string ip,int port)
        {
            Socket socket = NewSocketObj();
            //创建对象IP/PORT
            IPEndPoint point = new IPEndPoint(IPAddress.Parse(ip), port);
            //绑定端口号
            socket.Bind(point);
            //设置监听
            socket.Listen(10);
            return socket;
        }
        
        protected static Socket NewSocketObj()
        {
            return new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        }
    }
}
